﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Pibby.Core.Web;
using System.Xml.Serialization;
using System.IO;
using Pibby.Core.Persistence;
using Pibby.Core.IO;
using invrt;
using dogd;

namespace Pibby.Core
{
    /// <summary>
    /// Locally persisted application-wide settings saved to .xml file (rather than db).
    /// </summary>
    public class PerUserSettings : IUserSettings
    {
		#region Constructor

		public PerUserSettings()
		{
			this.MainWindow = new MainWindowSettings();
			this.Polling = new PollingSettings();

			this.ShowTooltips = true;
			this.TooltipTimeout = 500;
		}

        public PerUserSettings(IUserSettings settings)
			: this()
		{
			this.DownloadFolder = settings.DownloadFolder;
			this.StartMinimized = settings.StartMinimized;

			this.MainWindow.CurrentView = settings.MainWindow.CurrentView;
			this.MainWindow.Height = settings.MainWindow.Height;
			this.MainWindow.Left = settings.MainWindow.Left;
			this.MainWindow.SplitterDistance = settings.MainWindow.SplitterDistance;
			this.MainWindow.Top = settings.MainWindow.Top;
			this.MainWindow.Width = settings.MainWindow.Width;

			this.RepositoryPath = settings.RepositoryPath;
			this.RefreshAtStartup = settings.RefreshAtStartup;
			this.ShowTooltips = settings.ShowTooltips;
			this.TooltipTimeout = settings.TooltipTimeout;		
			this.Path = settings.Path;

			this.Polling.StartupDelayMinutes = settings.Polling.StartupDelayMinutes;
			this.Polling.PollingIntervalMinutes = settings.Polling.PollingIntervalMinutes;
		}

        public PerUserSettings(IFolder downloadFolder, IFolder settingsFolder, string dbFileName)
			: this()
		{
			this.DownloadFolder = downloadFolder.Path;
			this.RepositoryPath = settingsFolder.PathToFile(dbFileName);
		}

		#endregion Constructor

        /// <summary>
        /// How to start the main application window
        /// </summary>
        public bool StartMinimized
        {
            get;
            set;
        }

        /// <summary>
        /// Where the main window was left last time
        /// </summary>
        public MainWindowSettings MainWindow 
        { 
            get; 
            set; 
        }

        /// <summary>
        /// Where to find the database
        /// </summary>
        public string RepositoryPath
        {
            get;
            set;
        }

        /// <summary>
        /// Where downloads will be held before being sent to the media player
        /// </summary>
        public string DownloadFolder
        {
            get;
            set;
        }

        /// <summary>
        /// How often to refresh the feeds
        /// </summary>
        public PollingSettings Polling
        {
            get;
            set;
        }

        /// <summary>
        /// Convenient property containing the path to the file 
        /// - used for saving back again once the file has been loaded
        /// </summary>
        [XmlIgnore]
        public string Path
        {
            get;
            set;
        }

        /// <summary>
        /// Save the settings to an xml file
        /// </summary>
        public void Save(TextWriter writer)
        {
            XmlPersistence.SaveTo<PerUserSettings>(writer, this);
        }

        public void Save()
        {
            Contract.EnsureIsNotNullOrEmpty(this.Path);

            var streamFactory = InversionOfController.Factory.CreateTypeOf<ITextWriterFactory>();

            using (var writer = streamFactory.FromFile(this.Path))
            {
                this.Save(writer);
            }
        }

        public static PerUserSettings Load(TextReader reader)
        {
            return XmlPersistence.LoadFrom<PerUserSettings>(reader);
        }

        public static PerUserSettings Reload(string path)
        {
            Contract.EnsureIsNotNullOrEmpty(path);

            var streamFactory = InversionOfController.Factory.CreateTypeOf<ITextReaderFactory>();

            using (var reader = streamFactory.FromFile(path))
            {
                var settings = Load(reader);
                settings.Path = path;

                return settings;
            }
        }

        public bool RefreshAtStartup
        {
            get;
            set;
        }

        public bool ShowTooltips
        {
            get;
            set;
        }

        public int TooltipTimeout
        {
            get;
            set;
        }
    }
}
